package com.example.goodpet.chat.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.goodpet.R;
import com.example.goodpet.chat.activity.ChatActivity;
import com.example.goodpet.db.entity.User;
import com.example.goodpet.utils.Constants;
import com.example.goodpet.utils.SecurityUtils;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder>{
    private List<com.example.goodpet.db.entity.User> lists;
    private Context context;

    public UserAdapter(List<User> lists, Context context) {
        this.lists = lists;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_result_user, parent, false);
        return new UserAdapter.ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        com.example.goodpet.db.entity.User user = lists.get(position);
        Glide.with(context).load(Constants.BASE_URL_IMAGE+ user.getPortrait()).into(holder.portrait);
        holder.tvNickname.setText(user.getNickname());
        holder.tvSignature.setText(user.getSignature());
        if (user.getGender()==0)
            holder.ivSex.setImageDrawable(context.getDrawable(R.drawable.man));
        else if (user.getGender()==1)
            holder.ivSex.setImageDrawable(context.getDrawable(R.drawable.women));

        holder.layoutEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChatActivity.actionStart(context, SecurityUtils.encryptPhoneNumber(user.getPhoneNumber()),1);
            }
        });
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private CircleImageView portrait;
        private TextView tvNickname;
        private TextView tvSignature;
        private ImageView ivSex;
        private LinearLayout layoutEnter;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            portrait=itemView.findViewById(R.id.portrait);
            tvNickname=itemView.findViewById(R.id.tvNickname);
            tvSignature=itemView.findViewById(R.id.tvSignature);
            layoutEnter=itemView.findViewById(R.id.layoutEnter);
            ivSex=itemView.findViewById(R.id.ivSex);
        }
    }
}
